package vg.services.data_base_manager.data.graph;

import java.util.List;


/**
 * This class is bean for data of vertex.
 * Warning: No thread safety.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class Vertex extends AttributeItem implements Cloneable {
	// Main data
	private Graph innerGraph = null;
	
//=============================================================================
//-----------------Constructors------------------------------------------------
	public Vertex() {}
	
	public Vertex(List<Attribute> attributes) {
		super(attributes);
	}
	
	public Vertex(List<Attribute> attributes, Graph innerGraph) {
		super(attributes);
		this.innerGraph = innerGraph;
	}
	
	public Vertex(AttributeItem item, Graph innerGraph) {
		super(item);
		this.innerGraph = innerGraph;
	}
	
	public Vertex(AttributeItem item) {
		super(item);
	}
	
//=============================================================================
//-----------------PUBLIC METHODS----------------------------------------------
	/**
	 * This method adds subgraph in the vertex.
	 */
	public Graph createInnerGraph(boolean directed) {
		innerGraph = new Graph(directed);
		return innerGraph;
	}
	
	public boolean removeInnerGraph() {
		if (innerGraph != null) {
			innerGraph = null;
			return true;
		}
		return false;
	}
	
	public void setInnerGraph(Graph innerGraph) {
		this.innerGraph = innerGraph;
	}

	public Graph getInnerGraph() {
		return innerGraph;
	}
	
	public boolean isInnerGraph() {
		return innerGraph != null && innerGraph.getStorableId() >= 0;
	}
	
	public int getInnerGraphId() {
		if (isInnerGraph())
			return innerGraph.getStorableId();
		return -1;
	}
	
	@Override
	public Vertex clone() {
		if (innerGraph != null)
			return new Vertex(super.clone(), innerGraph.clone());
		else
			return new Vertex(super.clone());
	}
	
	@Override
	public int hashCode() {
		return getStorableId();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == this) return true;
		
		if (obj instanceof Vertex) {
			if (getStorableId() >= 0 && getStorableId() == ((Vertex)obj).getStorableId())
				return true;
		}		
		return false;
	}
}
